﻿using DataAccess;
using DataEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Aspose.Cells;

namespace WebUI.Controllers
{
    public class ReportController : Controller
    {
        //
        // GET: /Report/
        private DataContext db = new DataContext();
        private IEntityRepository<OffGoods> GoodsRepository;
        public ReportController()
        {
            GoodsRepository = new EntityRepository<OffGoods>(db);
        }
        public ActionResult Index()
        {
            return View();
        }
        #region 报表页面

        public ActionResult InventoryDetailReport()
        {
            return View();
        }
        #endregion

        #region 进出库明细json
        public ActionResult InventoryDetailJson(string date)
        {
            Dictionary<string, object> JsonObj = new Dictionary<string, object>();
            var Goods = GoodsRepository.Get(n => !n.BeLock).OrderBy(n => n.OffGoodsClassID).ThenBy(n => n.SortNumber).ToList();
            JsonObj.Add("total", Goods.Count());
            if (!string.IsNullOrWhiteSpace(date))
            {
                date = date + "-15";
            }
            DateTime TheDate = DateTime.Now;
            if (!DateTime.TryParse(date, out TheDate))
            {
                TheDate = DateTime.Now;
            }
            List<Dictionary<string, object>> JsonDetailObj = new List<Dictionary<string, object>>();
            Dictionary<string, object> DetailObj;
            foreach (var item in Goods)
            {
                DetailObj = new Dictionary<string, object>();
                
                //物品名称
                DetailObj.Add("GoodsName", item.Name);
                //上月库存数
                DetailObj.Add("LastMonthEndStock", BllGoods.GetLastMonthEndStock(item.ID, TheDate));
                //本月入存数
                DetailObj.Add("CurrentMonthPurchaseCount", BllGoods.GetCurrentMonthPurchaseCount(item.ID, TheDate));
                //本月出库数
                DetailObj.Add("CurrentMonthRequisitionCount", BllGoods.GetCurrentMonthRequisitionCount(item.ID, TheDate));
                //本月入库金额
                DetailObj.Add("CurrentMonthPurchaseTotalPrice", BllGoods.GetCurrentMonthPurchaseTotalPrice(item.ID, TheDate));
                //本月出库金恩
                DetailObj.Add("CurrentMonthRequisitionTotalPrice", BllGoods.GetCurrentMonthRequisitionTotalPrice(item.ID, TheDate));
                JsonDetailObj.Add(DetailObj);
            }
            JsonObj.Add("rows", JsonDetailObj);
            return JsonHelper.JsonEx(JsonObj);
        }
        #endregion

        #region Aspose导出
         [ValidateInput(false)]
        public void InventoryDetailExport(string date)
        {
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];
            Cells cells = sheet.Cells;
            //int Colnum = dt.Columns.Count;//表格列数 
            //int Rownum = dt.Rows.Count;//表格行数 

            #region 样式
            Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式
            styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
            styleTitle.Font.Name = "宋体";//文字字体 
            styleTitle.Font.Size = 12;//文字大小 
            //styleTitle.Font.IsBold = true;//粗体 

            //样式2 
            Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式 
            style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中 
            style2.Font.Name = "宋体";//文字字体 
            style2.Font.Size = 12;//文字大小 
            //style2.Font.IsBold = true;//粗体 
            //style2.IsTextWrapped = true;//单元格内容自动换行 
            //style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            //style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            //style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            //style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

            //样式3 
            Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式 
            style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中 
            style3.Font.Name = "宋体";//文字字体 
            style3.Font.Size = 10;//文字大小
            #endregion
            #region 标头
            cells.Merge(0, 0, 2, 1);//合并单元格
            cells[0, 0].PutValue("物品名称");//填写内容 
            cells[0, 0].SetStyle(styleTitle); 
            cells.SetRowHeight(0, 20);
            cells.SetColumnWidth(0, 50);
            cells.Merge(0, 1, 2, 1);//合并单元格
            cells[0, 1].PutValue("上月末库存数");//填写内容 
            cells[0, 1].SetStyle(styleTitle);
            cells.SetRowHeight(0, 20);
            cells.SetColumnWidth(0, 80);

            cells.Merge(0, 2, 1, 2);//合并单元格
            cells[0, 2].PutValue("本月入库");//填写内容 
            cells[0, 2].SetStyle(styleTitle);
            cells.SetRowHeight(0, 20);
            cells.SetColumnWidth(0, 80);

            cells.Merge(0, 4, 1, 2);//合并单元格
            cells[0, 4].PutValue("本月出库");//填写内容 
            cells[0, 4].SetStyle(styleTitle);
            cells.SetRowHeight(0, 20);
            cells.SetColumnWidth(0, 80);

           

            
            cells[1, 2].PutValue("入库数量");//填写内容 
            cells[1, 2].SetStyle(styleTitle);
            cells.SetRowHeight(0, 20);
            cells.SetColumnWidth(0, 40);

            
            cells[1, 3].PutValue("入库金额");//填写内容 
            cells[1, 3].SetStyle(styleTitle);
            cells.SetRowHeight(0, 20);
            cells.SetColumnWidth(0, 40);

           
            cells[1, 4].PutValue("出库数量");//填写内容 
            cells[1, 4].SetStyle(styleTitle);
            cells.SetRowHeight(0, 20);
            cells.SetColumnWidth(0, 40);

           
            cells[1, 5].PutValue("出库金额");//填写内容 
            cells[1, 5].SetStyle(styleTitle);
            cells.SetRowHeight(0, 20);
            cells.SetColumnWidth(0, 40);
            #endregion
            #region 物品数据
            var Goods = GoodsRepository.Get(n => !n.BeLock).OrderBy(n => n.OffGoodsClassID).ThenBy(n => n.SortNumber).ToList();
            if (!string.IsNullOrWhiteSpace(date))
            {
                date = date + "-15";
            }
            DateTime TheDate = DateTime.Now;
            if (!DateTime.TryParse(date, out TheDate))
            {
                TheDate = DateTime.Now;
            }
            for (int i = 0; i < Goods.Count; i++)
            {
                cells[i+2, 0].PutValue(Goods[i].Name);
                cells[i+2, 0].SetStyle(style3);
                cells[i+2, 1].PutValue(BllGoods.GetLastMonthEndStock(Goods[i].ID, TheDate));
                cells[i+2, 2].SetStyle(style3);
                cells[i+2, 2].PutValue(BllGoods.GetCurrentMonthPurchaseCount(Goods[i].ID, TheDate));
                cells[i+2, 2].SetStyle(style3);
                cells[i+2, 3].PutValue(BllGoods.GetCurrentMonthPurchaseTotalPrice(Goods[i].ID, TheDate));
                cells[i+2, 3].SetStyle(style3);
                cells[i+2, 4].PutValue(BllGoods.GetCurrentMonthRequisitionCount(Goods[i].ID, TheDate));
                cells[i+2, 4].SetStyle(style3);
                cells[i+2, 5].PutValue(BllGoods.GetCurrentMonthRequisitionTotalPrice(Goods[i].ID, TheDate));
                cells[i+2, 5].SetStyle(style3);
            }
            #endregion
            workbook.Save(System.Web.HttpContext.Current.Response, date + ".xls", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));


            System.Web.HttpContext.Current.Response.End();

        }

        #endregion

    }
}
